import {prePathBloc} from '@/store/module/PrePath';

export const on = (function () {
  if (!!document.addEventListener) {
    return function (element, event, handler) {
      if (element && event && handler) {
        element.addEventListener(event, handler, false)
      }
    }
  } else {
    return function (element, event, handler) {
      if (element && event && handler) {
        element.attachEvent('on' + event, handler)
      }
    }
  }
})()

/**
 * @description 解绑事件 off(element, event, handler)
 */
export const off = (function () {
  if (!!document.removeEventListener) {
    return function (element, event, handler) {
      if (element && event) {
        element.removeEventListener(event, handler, false)
      }
    }
  } else {
    return function (element, event, handler) {
      if (element && event) {
        element.detachEvent('on' + event, handler)
      }
    }
  }
})()
export const getToday = () => {
  let now = new Date(); // 当前日期
  let nowDay = now.getDate(); // 当前日
  let nowMonth = now.getMonth() + 1; // 当前月
  let nowYear = now.getFullYear(); // 当前年
  let hour = now.getHours();
  let minute = now.getMinutes();
  let second = now.getSeconds();
  let month = ''
  let day = ''
  let hours = ''
  let minutes = ''
  let seconds = ''

  nowYear += (nowYear < 2000) ? 1900 : 0; //
  if (nowMonth < 10) {
    month = '0' + nowMonth;
  } else {
    month = nowMonth.toString()
  }
  if (nowDay < 10) {
    day = '0' + nowDay;
  } else {
    day = nowDay.toString()
  }
  if (hour < 10) {
    hours = '0' + hour;
  } else {
    hours = hour.toString()
  }
  if (minute < 10) {
    minutes = '0' + minute;
  } else {
    minutes = minute.toString()
  }
  if (second < 10) {
    seconds = '0' + second;
  } else {
    seconds = second.toString();
  }

  return nowYear + '/' + month + '/' + day + ' ' + hours + ':' + minutes + ':' + seconds;
};
//返回上一级导航
const needCloseUrl = [
  {
    current: '/member/detail',
    pre: '/member'
  },
  {
    current: '/member/DetailDetail',
    pre: '/member'
  },
  {
    current: '/device/nav',
    pre: '/device'
  },
  {
    current: '/device/detail',
    pre: '/device-manage/devices'
  },
  {
    current: '/family/detail',
    pre: '/family'
  },
  {
    current: '/family/DetailDetail',
    pre: '/family'
  },
  {
    current: '/maintain/detail',
    pre: '/service/nav/maintain'
  },
  {
    current: '/apply/detail',
    pre: '/service/nav/apply'
  },
  {
    current: '/news/detail/detail',
    pre: '/news/list'
  },
  {
    current: '/order/detail',
    pre: '/order'
  },
  {
    current: '/maintainRecord/detail',
    pre: '/service/nav/maintainRecord'
  },
  {
    current: '/banners/wishesList/detail',
    pre: '/banners/nav/wishes'
  },
  {
    current: '/family',
    pre: '/member'
  },
  {
    current: '/member/Point',
    pre: '/member'
  },
  {
    current:'/questionnaire/edit',
    pre:'/questionnaire'
  },
  {
    current:'/questionnaire/detail',
    pre:'/questionnaire'
  },
  {
    current:'/questionnaire/create',
    pre:'/questionnaire'
  },
  {
    current:'/commentmanagement',
    pre:'/management'
  },
  {
    current:'/device-manage/restory',
    pre:'/device-manage/devices'
  },
  {
    current:'/coupons/member',
    pre:'/coupons'
  },
  {
    current:'/personinfo',
    pre:'/member'
  },
  {
    current:'/point',
    pre:'/member'
  },
  {
    current:'/health',
    pre:'/member'
  },
  {
    current:'/associateddevices',
    pre:'/member'
  },
  {
    current:'/usagerecords',
    pre:'/member'
  },
  {
    current:'/salesrecords',
    pre:'/member'
  },
  {
    current:'/orderrecords',
    pre:'/member'
  },
  {
    current:'brainwave/detail',
    pre:'/brainwave'
  },

];
export const handleRouteClosebtn = (to: any, from: any) => {
  const idx = needCloseUrl.findIndex(x => to.path.includes(x.current));
  if (idx === -1) {
    prePathBloc.getPrePath('');
    return;
  }
  if (from.path !== '/' && to.path.includes(needCloseUrl[1].current)) prePathBloc.getPrePath(from.path);
  else prePathBloc.getPrePath(needCloseUrl[idx].pre)
};
